﻿using Newtonsoft.Json;
using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.BLL.Service;
using QG.FrontDesk.com.Attbutes;
using QG.FrontDesk.com.Models;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace QG.FrontDesk.com.Controllers
{

    public class PositionController : Controller
    {
        private IPositionSubscService _positionSubscService;

        private ICityInfoService _cityInfoService;

        private IIndustryService _industryService;

        private IDevelopmentPhaseService _developmentPhaseService;

        private ICompanyService _companyService;

        private IPositionInfoService _positionInfoService;

        private IPosition_ResumeinfoService _position_Resumeinfo;

        public PositionController(IPositionSubscService positionSubscService, ICityInfoService cityInfoService,
            IIndustryService industryService, IDevelopmentPhaseService developmentPhaseService,
            ICompanyService companyService, IPositionInfoService positionInfoService, IPosition_ResumeinfoService position_Resumeinfo)
        {
            DataContext<Company>.context.model = null;
            DataContext<UserInfo>.context.model = null;
            //获取存在的cookie
            //再次浏览器判断是否有值
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (cookie != null)
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (ticket.Name == "求职")
                {
                    DataContext<UserInfo>.context.model = JsonConvert.DeserializeObject<UserInfo>(userData);
                }
                else if (ticket.Name == "公司")
                {
                    DataContext<Company>.context.model = JsonConvert.DeserializeObject<Company>(userData);
                }
            }
            this._positionSubscService = positionSubscService;
            this._cityInfoService = cityInfoService;
            this._industryService = industryService;
            this._developmentPhaseService = developmentPhaseService;
            this._companyService = companyService;
            _positionInfoService = positionInfoService;
            _position_Resumeinfo = position_Resumeinfo;
        }

        ResponseResult responseResult = new ResponseResult();

        /// <summary>
        /// 职位订阅
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult Subscribe()
        {
            List<Classifyinfo> classifyinfo = _positionSubscService.GetClassify();
            List<PostName> postNames = _positionSubscService.GetPostName();
            List<Cityinfo> cityinfos = _cityInfoService.GetCityinfos();
            List<DevelopmentPhase> developments = _developmentPhaseService.GetDevelopmentPhases();
            ViewBag.classifyinfo = classifyinfo;
            ViewBag.postNames = postNames;
            ViewBag.cityinfos = cityinfos;
            ViewBag.developments = developments;

            List<Industry> industries = _industryService.GetIndustries();
            ViewBag.industries = industries;
            return View();
        }


        //职位订阅——新增一条职位订阅记录
        public JsonResult AddPositionSubscribe(PositionSubscribe positionSubscribe)
        {
            responseResult.success = _positionSubscService.AddPositionSubscribe(positionSubscribe);
            return Json(responseResult);
        }


        /// <summary>
        /// 收藏的职位
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult Collections()
        {
            int UserId = DataContext<UserInfo>.context.model.UserId;
            List<CollectPost> collect = _positionSubscService.GetCollectPost(UserId) ?? new List<CollectPost>();

            ViewBag.collect = collect;
            return View();
        }

        /// <summary>
        /// 发布新职位
        /// </summary>
        /// <returns></returns>
        [ValidateInput(false)]
        [PermissionAttribute]
        public ActionResult Create()
        {
            List<PostName> postNames = _positionSubscService.GetPostName();
            List<Classifyinfo> classifyinfo = _positionSubscService.GetClassify();
            ViewBag.cityinfos = _cityInfoService.GetCityinfos();
            ViewBag.postNames = postNames;
            ViewBag.classifyinfo = classifyinfo;
            ViewBag.Email = DataContext<Company>.context.model.CompanyEmail;
            return View();
        }

        /// <summary>
        /// 职位发布成功
        /// </summary>
        /// <returns></returns>
        public ActionResult PublishSuccess()
        {
            return View();
        }
        /// <summary>
        /// 职位详情(未登录)
        /// </summary>
        /// <returns></returns>
        public ActionResult Jobdetail(int postId)
        {
            //判断是否已登录
            if (DataContext<UserInfo>.context.model != null || DataContext<Company>.context.model != null)
            {
                return RedirectToAction("Jobdetail1", new { postId = postId });
            }
            PositionInfo positionInfo = _positionSubscService.GetPositionInfo(postId, 0);
            positionInfo.Company = _companyService.GetCompanyInfo(positionInfo.CompId);//得到对应的公司信息
            ViewBag.positionInfo = positionInfo;
            return View();
        }
        /// <summary>
        /// 职位详情(已登录)
        /// </summary>
        /// <returns></returns>
        public ActionResult Jobdetail1(int postId)
        {
            //判断是否已登录
            if (DataContext<UserInfo>.context.model == null && DataContext<Company>.context.model == null)
            {
                return RedirectToAction("Jobdetail", new { postId = postId });
            }

             UserInfo user= DataContext<UserInfo>.context.model??new UserInfo();
            PositionInfo positionInfo = _positionSubscService.GetPositionInfo(postId, user.UserId);
            positionInfo.Company = _companyService.GetCompanyInfo(positionInfo.CompId);//得到对应的公司信息
            ViewBag.positionInfo = positionInfo;

            return View();
        }

        /// <summary>
        /// 收藏职位——新增一条收藏职位表记录
        /// </summary>
        /// <param name="collectPost">收藏职位表</param>
        /// <returns></returns>
        public JsonResult AddCollection(CollectPost collectPost,int PostId)
        {
            collectPost.UserId = DataContext<UserInfo>.context.model.UserId;
            collectPost.PostId = PostId;
            responseResult.success = _positionSubscService.AddCollection(collectPost);
            return Json(responseResult);
        }

        /// <summary>
        /// 收藏职位——取消收藏
        /// </summary>
        /// <param name="collectPost"></param>
        /// <returns></returns>
        public JsonResult DelectCollection(CollectPost collectPost)
        {
            collectPost.UserId =DataContext<UserInfo>.context.model.UserId;
            responseResult.success = _positionSubscService.DelectCollection(collectPost);
            return Json(responseResult);

        }


        /// <summary>
        /// 更多职位
        /// </summary>
        /// <returns></returns>
        
        public ActionResult List(Nullable<int> ClassId, Nullable<int> level, string type, string word, int page = 1)
        {
            List<PositionInfo> positions = null;
            //得到职位消息
            if (level != null)
            {
                positions = _positionInfoService.GetPositionInfos(null, null, ClassId, level) ?? new List<PositionInfo>();
            }
            else if (!string.IsNullOrEmpty(word))
            {
                positions = _positionInfoService.GetPositionInfos(type, word, null, null) ?? new List<PositionInfo>();

            }
            else
            {
                positions = _positionInfoService.GetPositionInfos(null, null, null, null) ?? new List<PositionInfo>();
            }
            ViewBag.Count = positions.Count % 10 > 0 ? positions.Count / 10 + 1 : positions.Count / 10;//总页数
            ViewBag.positions = positions.Skip((page - 1) * 10).Take(10).ToList();//进行筛选分页
            ViewBag.curr = page;//当前页数
            ViewBag.word = word ?? "";
            //得到对应的城市
            ViewBag.cityinfos = _cityInfoService.GetCityinfos() ?? new List<Cityinfo>();
            return View();
        }
        /// <summary>
        /// 推荐职位
        /// </summary>
        /// <returns></returns>
        public ActionResult MList()
        {
            return View();
        }
        /// <summary>
        /// 有效职位
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult ValidPositions()
        {
            int CompId = DataContext<Company>.context.model.CompId;
            ViewBag.positions = _positionInfoService.GetPosition(CompId, true);
            return View();
        }
        /// <summary>
        /// 下线职位
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult InvalidPositions()
        {
            int CompId = DataContext<Company>.context.model.CompId;
            ViewBag.positions = _positionInfoService.GetPosition(CompId, false);
            return View();
        }

        /// <summary>
        /// 修改职位状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="statu"></param>
        /// <returns></returns>
        public JsonResult UpdatePositionStatu(int id, bool statu)
        {
            responseResult.success = _positionInfoService.UpdatePositionStatu(id, statu) > 0;
            return Json(responseResult);
        }

        /// <summary>
        /// 订阅详情
        /// </summary>
        /// <returns></returns>
        public ActionResult SubscribeDetails()
        {
            return View();
        }


        /// <summary>
        /// 新增职位信息
        /// </summary>
        /// <returns></returns>
        public JsonResult AddPosition(PositionInfo position, PostName postName)
        {
            position.MinSalary = position.MinSalary * 1000;
            position.MaxSalary = position.MaxSalary * 1000;
            position.CompId =DataContext<Company>.context.model.CompId;
            responseResult.success = _positionSubscService.AddPosition(position, postName) > 0;
            return Json(responseResult);
        }

        /// <summary>
        /// 投递简历
        /// </summary>
        /// <param name="position_Resume"></param>
        /// <returns></returns>
        public JsonResult AddPosition_Resumeinfo(Position_ResumeInfo position_Resume)
        {
            UserInfo user = DataContext<UserInfo>.context.model ?? new UserInfo();
            //进行自动筛选操作
            if (!_position_Resumeinfo.IsAccord(position_Resume.PostId, user.UserId))
            {
                position_Resume.Statu = 5;
            }

            responseResult.success = _position_Resumeinfo.AddPosition_Resumeinfo(position_Resume, user.UserId) > 0;
            responseResult.msg = responseResult.success ? "投递成功" : "投递失败";
            return Json(responseResult);
        }

    }
}
